package interview.gc;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

/**
 * @Author: cctv
 * @Date: 2019/5/16 15:29
 */
public class Finalization {
    public static Finalization finalization;

    @Override
    protected void finalize() {
        System.out.println("Finalized");
        finalization = this;
    }

    public static void main(String[] args) {
        Finalization f = new Finalization();
        System.out.println("First print:" + f);
        f = null;
        System.gc();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Second print:" + f);
        System.out.println("last print:" + finalization);

        //强引用
        String str = new String("abc");
        //软引用
        SoftReference<String> softRef = new SoftReference<>(str);
        //弱引用
        WeakReference<String> weakRef = new WeakReference<>(str);
        //虚引用
        ReferenceQueue queue = new ReferenceQueue();
        PhantomReference ref = new PhantomReference(str, queue);
    }
}
